package zdar.processing;

import zdar.entities.Action2;
import zdar.entities.Attack;
import zdar.entities.CardDeck;
import zdar.entities.MeleeAction;
import zdar.entities.RangeAction;
import zdar.entities.Unit;
import zdar.entities.UnitTemplate;

/**
 * Not exactly sure how to approach this class yet. 
 * Currently it just says who the target is and sends an attack their way when triggered.
 * Basically the game screen is responsible for setting the target (the source is not needed)
 * 
 * But maaaybe, if we handle all the main combat aspects here it will help keep Fight Screen more clear for screen relatd things.
 * So we give the CombatProcessor a list of all active units (well just the two for now) and it will control initiative and things.
 * @author danbye50
 *
 */
public class CombatProcessor {

	private Unit target, player, enemy;
	private int rounds, turns, maxTurns;
	
	private CardDeck cards;
	
	public CombatProcessor(Unit player, Unit enemy){
		cards = new CardDeck();
		rounds = 0;
		turns = 0;
		this.player = player;
		this.enemy = enemy;
		maxTurns = 2;
		
		drawInitiative();
	}
	
	private void drawInitiative(){
		cards.shuffle();
		if (player.drawCard(cards) > enemy.drawCard(cards)){
			setTarget(enemy);
		} else {
			setTarget(player);
		}
	}
	
	private void setTarget(Unit target){
		this.target = target;
	}
	
	public boolean playerTurn(){
		if (target != player){	// There is probably a better way to do this.
			return true;
		} else {
			return false;
		}
	}
	public void endTurn(){
		turns++;
		if (turns == maxTurns){
			turns = 0;
			rounds++;
			drawInitiative();
		} else {
			if (target==player){
				target = enemy;
			} else {
				target = player;
			}
		}
	}
	
	public void process(Action2 action){
		if (action.getClass() == MeleeAction.class){
			processMelee((MeleeAction) action);
		}
		else if (action.getClass() == RangeAction.class){
			processRange((RangeAction) action);
		}
		else{
			System.out.println("who's putting untagged Actions in places they shouldn't be?");
		}
	}
	
	private void processMelee(MeleeAction action){
		System.out.println("Processing Melee Attack!");
		Attack attack = action.getAttack(getTargetRange());
		target.receiveMeleeAttack(attack);
	}
	
	private void processRange(RangeAction action){
		System.out.println("Processing Ranged Attack!");
		Attack attack = action.getAttack(getTargetRange());
		target.receiveRangeAttack(attack);
	}
	
	private int getTargetRange(){
		// Now how are we going to do this...
		// Doesn't matter for now, everything will be at point blank
		return 0;
	}
	
	/*private int getCoverPenalty(){
		
		return 0;
	}*/
	
	public void getDisplayInfo(){
		
	}
}
